package com.dgut.uhrs.controller;

import com.dgut.uhrs.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "/admin/toAdminIndex")
    public String toAdminIndex(Authentication authentication){
        return "adminIndex";
    }

    @RequestMapping(value = "/adminLoginWithUserId")
    public String adminLoginWithUserId(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user = (User) authentication.getPrincipal();
        List<String> roles = adminService.adminLoginWithUserId(user.getUsername());
        if (roles.get(0).equals("ROLE_ADMIN") || roles.get(1).equals("ROLE_ADMIN")){
            return "adminIndex";
        }
        else {
            return null;
        }
    }
    @RequestMapping(value = "/admin/toAdminLogin" )
    public String toAdminLogin(){
        return "adminLogin";
    }

}
